package me.firstapp.manage.advertisement;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.json.AbstractJsonObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonAdvertisement;
import me.firstapp.common.utils.DateUtils;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.manage.common.BaseManage;
import me.firstapp.manage.utils.BizData4Page;
import me.firstapp.spi.bbs.AdvertisementService;

@Controller
@RequestMapping(value = "/advertisementManage")
public class AdvertisementManageAct extends BaseManage {

	@Autowired
	private AdvertisementService advertisementService;

	@ResponseBody
	@RequestMapping(value = "/findAdvertisementPage.do", method = RequestMethod.POST)
	public void findAdvertisementPage(@RequestParam Map<String, Object> conditions, HttpServletRequest request,
			HttpServletResponse response) {

		try {
			// 搜索条件
			Map<String, Object> searchParams = new HashMap<>();
			String sSearchJson = (String) conditions.get("sSearch");
			if (StrUtils.isNotNULL(sSearchJson)) {
				JSONObject jsonObject = JSONObject.parseObject(sSearchJson);
				if (jsonObject != null) {
					Set<String> keySet = jsonObject.keySet();
					for (String key : keySet) {
						searchParams.put(key, jsonObject.get(key));
					}
				}
			}
			// 排序字段
			String mDataProp = "mDataProp_" + conditions.get("iSortCol_0");
			mDataProp = conditions.get(mDataProp).toString().replace("Str", "");
			// 排序方式
			String sSortDir_0 = (String) conditions.get("sSortDir_0");
			// 页容
			String iDisplayLengthStr = (String) conditions.get("iDisplayLength");
			// 是开始数不是当前页数...
			String iDisplayStartStr = (String) conditions.get("iDisplayStart");
			if (StrUtils.isNotNULL(iDisplayLengthStr) && StrUtils.isNotNULL(iDisplayStartStr)) {
				Integer iDisplayLength = Integer.valueOf(iDisplayLengthStr);
				Integer iDisplayStart = Integer.valueOf(iDisplayStartStr);
				// 当前页数
				Integer pageNo = iDisplayStart / iDisplayLength + 1;
				PageObject<JsonAdvertisement> adPage = advertisementService.findAdsPage(pageNo, iDisplayLength,
						searchParams, mDataProp, sSortDir_0);
				SingleObject<Object> result = new SingleObject<Object>();
				BizData4Page<JsonAdvertisement> bizData4Page = new BizData4Page<JsonAdvertisement>();
				bizData4Page.setUserdata(conditions);
				if (adPage.getPage() != null) {
					bizData4Page.setPage(adPage.getPage().getPageNo());
					bizData4Page.setPagesize(adPage.getPage().getPageSize());
					bizData4Page.setRecords(adPage.getPage().getTotalCount());
					bizData4Page.setRows(new ArrayList<JsonAdvertisement>(adPage.getPage().getList()));
					bizData4Page.setTotal(adPage.getPage().getTotalCount());
				}

				result.setCode(adPage.getCode());
				result.setMsg(adPage.getMsg());
				result.setObject(bizData4Page);
				ResponseUtils.renderJson(response, result);
			}
		} catch (Exception e) {
			e.printStackTrace();
			AbstractJsonObject abstractJsonObject = new AbstractJsonObject();
			abstractJsonObject.setCode("error");
			abstractJsonObject.setMsg("发生未知错误");
			ResponseUtils.renderJson(response, abstractJsonObject);
		}
	}

	@RequestMapping(value = "/addAdvertisement.do", method = RequestMethod.POST)
	public void addAdvertisement(String title, String description, String image, String url, String startDate,
			String endDate, Integer sort, HttpServletRequest request, HttpServletResponse response) {
		Date _startDate = DateUtils.getDateFromDateStr(startDate, "yyyy-MM-dd");
		Date _endDate = DateUtils.getDateFromDateStr(endDate, "yyyy-MM-dd");
		SingleObject<JsonAdvertisement> result = advertisementService.addAdvertisement(title, description, image,
				url, _startDate.getTime(), _endDate.getTime(), sort);
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/updateAdvertisement.do", method = RequestMethod.POST)
	public void updateAdvertisement(Long id, String title, String description, String image, String url,
			String startDate, String endDate, Integer status, Integer sort, HttpServletRequest request,
			HttpServletResponse response) {
		Date _startDate = null;
		Long start = null;
		if (StrUtils.isNotNULL(startDate)) {
			_startDate = DateUtils.getDateFromDateStr(startDate, "yyyy-MM-dd");
			start = _startDate.getTime();
		}
		Date _endDate = null;
		Long end = null;
		if (StrUtils.isNotNULL(endDate)) {
			_endDate = DateUtils.getDateFromDateStr(endDate, "yyyy-MM-dd");
			end = _endDate.getTime();
		}

		SingleObject<JsonAdvertisement> result = advertisementService.updateAdvertisement(id, title, description,
				image, url, start, end, status, sort);
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/deleteAdvertisement.do", method = RequestMethod.POST)
	public void deleteAdvertisement(String ids, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = advertisementService.deleteAdvertisements(ids);
		ResponseUtils.renderJson(response, result);
	}
}
